#include "frm_querypage.h"
#include "ui_frm_querypage.h"
#include <QListView>
#include "queryresultmodel.h"
frm_QueryPage::frm_QueryPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::frm_QueryPage)
{
    ui->setupUi(this);
    connect(ui->sb_page,&QSpinBox::valueChanged,this,frm_QueryPage::PageChanged);
    connect(ui->btn_return,&QPushButton::clicked,this,&frm_QueryPage::returnToMain);
    //connect(ui->pushButton,&QPushButton::clicked,this,&frm_QueryPage::onOrder);
    connect(v1,&OneWayDelegate::bookButtonClicked,this,&frm_QueryPage::RequestForOrder);
}
void frm_QueryPage::setTable(QueryResultModel* fl){

    if(OneWayQueryResultModel* res = dynamic_cast<OneWayQueryResultModel*>(fl)){
        l1->setModel(res);
        v1->setTargetRole(OneWayResultPtrRole);
        l1->setItemDelegate(v1);
        l1->show();
        ui->horizontalLayout->addWidget(l1);

    }else if(RndTripResultModel* res = dynamic_cast<RndTripResultModel*>(fl)){
        v1->setTargetRole(CustomRoles::RndFirstRole);
        v2->setTargetRole(CustomRoles::RndSecondRole);
        l1->setModel(res);
        l2->setModel(res);
        l1->setItemDelegate(v1);
        l2->setItemDelegate(v2);
        l1->show();
        l2->show();
        ui->horizontalLayout->addWidget(l1);
        ui->horizontalLayout->addWidget(l2);
    }else if(TransferResultModel* res = dynamic_cast<TransferResultModel*>(fl)){
        v1->setTargetRole(CustomRoles::TransferFirstRole);
        v2->setTargetRole(CustomRoles::TransferSecondRole);
        l1->setModel(res);
        l2->setModel(res);
        l1->setItemDelegate(v1);
        l2->setItemDelegate(v2);
        ui->horizontalLayout->addWidget(l1);
        ui->horizontalLayout->addWidget(l2);
        l1->show();
        l2->show();
    }else if(RefundResultModel* res = dynamic_cast<RefundResultModel*>(fl)){
        l1->setModel(fl);
        ui->horizontalLayout->addWidget(l1);
        l1->setItemDelegate(new RefundResultModel::OrderDelegate(this));
        l1->show();
        ui->pushButton->setVisible(false);
    }
}

void frm_QueryPage::clean(){
    ui->horizontalLayout->removeWidget(l1);
    l1->hide();
    ui->horizontalLayout->removeWidget(l2);
    l2->hide();
    ui->pushButton->setVisible(true);
    ui->horizontalLayout->update();
}
void frm_QueryPage::setPage(int i){
    ui->sb_page->setValue(i);
}
frm_QueryPage::~frm_QueryPage()
{
    delete ui;
}
QStyledItemDelegate* frm_QueryPage::getViewDelegate(){
    return new OneWayDelegate();
}
